<%@ page import="com.google.appengine.api.users.*" %>
<%@ page import="com.google.appengine.api.datastore.*"%>
<%@ page import="transactionExchange.*" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html lang="en">
  <head>
    <link rel="stylesheet" href="/css/style.css" type="text/css">
    <title>Online Transaction Exchange</title>
  </head>


  <body>
    <div class="wrapper">
        <div class="google-header-bar">
            <div class="header content clearfix">
            <a id="link-google" href="http://riyadh.gtugs.org" >
            <img class="logo" src="/images/exchange_icon.png" alt="GTUG">
            </a>

            <%

            User user = UserServiceFactory.getUserService().getCurrentUser();
            if(user == null) {
            %>
            <a id="link-signup" class="signup" href="<%=UserServiceFactory.getUserService().createLoginURL("/")%>">Login</a>
            <%
            } else {
            %>
            <a id="link-signup" class="signup" href="<%=UserServiceFactory.getUserService().createLogoutURL("/")%>">Logout <%=user.getNickname()%></a>
            <%
            }
            %>
            </div>
        </div>


        <%

    // Form intelligence is put here so that the evaluation is done before drawing the table to have it updated.

    if ("true".equals(request.getParameter("process"))) {  //If submit button is pressed...

        String bookName,bookAuthor;

        if(request.getParameter("name") == null) {bookName = "Blank";}
            else {bookName = request.getParameter("name");}

        if(request.getParameter("author") == null) {bookAuthor = "Blank";}
            else {bookAuthor = request.getParameter("author");}

        String nickName = UserServiceFactory.getUserService().getCurrentUser().getNickname();
        new Book(bookName,bookAuthor,nickName);     //call the static constructor of the class Book to store data.
        new gtugBookList.Email(user.getEmail(),bookName,bookAuthor); //Call the constructor of the class Email to send email.
        }

      if ("del".equals(request.getParameter("process"))) {    //if del link is pressed...
         Book.delBook(KeyFactory.stringToKey(request.getParameter("key")));
        }

%>

  <div class="main content clearfix">
    <div class="sign-in">
        <div class="signin-box">
        <h2>Add Book<strong></strong></h2>
            <%                              //not to show the input fields if the user isn't logged in...
    if (UserServiceFactory.getUserService().getCurrentUser() == null) {%><a id="link-signup" class="signup">Login to post books</a><%}
                else {%>
            <form action='<%=request.getRequestURI()%>' method="POST">

            <p><b>Book Name</b><input type="text" name="name">
            <b>Author</b><input type="text" name="author">
                <br>
            <button type="submit">Add Book</button>
            <input type="HIDDEN" name="process" value="true">
            </form>
            <%} %>


        <ul><li></li></ul>
        </div>
     </div>

    <div class="product-info ah">
        <div  class="product-headers">
        <h1>Book List- Riyadh GTUG competition project</h1>
            <p></p>

                    <table>
                    <tr><td width="30"></td>
                        <td width="200"><b>Book Name</b></td>
                        <td width="200"><b>Book Author</b></td>
                        <td width="150"><b>User</b></td>
                        <td width="300"><b>Time</b></td></tr>

                    <%
                    for (Entity book : Book.queryData("User")) {  //Prepare query sort ascending by User.
                    %>

                    <tr><td><a href="index.jsp?process=del&key=<%=KeyFactory.keyToString((Key) book.getProperty("Key"))%>"><font size="1">del</font></a></td>
                        <td><font size="3"><%=(String) book.getProperty("Book Name")%></font></td>
                        <td><font size="3"><%=(String) book.getProperty("Book Author")%></font></td>
                        <td><font size="3"><%=(String) book.getProperty("User")%></font></td>
                        <td><font size="2"><%=book.getProperty("Date")%></font></td></tr>

                    <% } %>
                    </table>
        </div>
        <p>
         The content of this page is viewed best on Google Chrome Browser.
        </p>
    </div>

    <div id="cc_iframe_parent"></div>
  </div>


  <div class="announce-bar">
    <div class="content">
    <p class="message">
    Welcome to Omar's Book Listing demo page.
    <a target="_blank" href="http://code.google.com/appengine/">Read more</a>
    </p>
    </div>
  </div>


  <div class="google-footer-bar">
    <div class="footer content clearfix">
    <ul>
    <li>© 2011 Google Application Engine, Demo made -partially- by Omar A. AlSadoon</li>
    <li><a href="http://www.google.com/" >Google Home</a></li>
    </ul>
    </div>
  </div>
</div>

</body>
</html>